<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Teacher Extends MY_Controller
{	
	public function __construct()
	{
		parent::__construct();
		$this->page_auth->secure_page('teacher');
		$this->load->helper('misc');
		$this->load->model('M_teachers','mt');
		$this->view_data['system_message'] = $this->_msg();
		$this->view_data['current_url'] =site_url($this->router->class.'/'.$this->router->method);
	}
	
	
	public function index()
	{

	}
	
	public function print_card($hash = '')
	{
		$x = $this->hs->decrypt($hash);
		if($x)
		{

		}else{

		}
	}

	public function my_drive()
	{
		$this->layout_view = 'application_less_sidebar';
	}

	public function my_classes()
	{
		
		$this->view_data['class_list'] = $this->mt->get_classes();
	}
	
	public function my_schedules()
	{
		$this->view_data['schedules'] = $this->mt->get_schedules();
	}

	public function advisory_input_grades($hash = '')
	{
		$x = $this->hs->decrypt($hash);
		if($x)
		{
			$this->view_data['section_exists'] = TRUE;
			$this->load->model(['M_gperiods','M_sections']);
			$this->layout_view = 'application_less_sidebar';
			$this->view_data['subject'] = $this->mt->get_teacher_advisory_and_students($x[0]);
			$this->view_data['section_desc'] = $this->M_sections->get_all_sections($x[0]);
			$this->view_data['gp'] = $this->M_gperiods->get_gperiods();
			$this->view_data['hash'] = $hash;
		}else{
			$this->view_data['section_exists'] = FALSE;
			$this->view_data['hash'] = NULL;
		}
		
		$this->view_data['sections'] = $this->mt->_get_all_advisory_sections_for_dd();
	}

	public function advisory_section($hash = '',$enrollment_id = '')
	{
		$this->load->helper('grade');
		$this->layout_view = 'application_less_sidebar';
		$x = $this->hs->decrypt($hash);
		$e = $this->hs->decrypt($enrollment_id);

		if($x)
		{
			$sec_id = isset($x[0]) ? $x[0] : 0;
			$enr_id = isset($e[0]) ? $e[0] : 0;

			$this->view_data['section_exists'] = TRUE;
			$this->load->model(['M_gperiods','M_sections','M_enrollees']);
			$this->view_data['section_desc'] = $sd = $this->M_sections->get_the_section($sec_id);
			$this->view_data['gp'] = $this->M_gperiods->get_gperiods();
			$this->view_data['hash'] = $hash;
			$this->view_data['edit_grade'] = TRUE;
			$this->view_data['edit_remarks'] = TRUE;
			$this->view_data['edit_attendance'] = TRUE;
			$this->view_data['edit_all_period'] = FALSE;
			$this->view_data['students'] = $this->mt->get_students($sec_id);
			$this->view_data['section_level_id'] = __link($this->mt->get_section_assigned_grade_level($sec_id)); 
			$this->view_data['enrollee_data'] = $en = $this->M_enrollees->get_enrollee_prof($enr_id);
			$this->view_data['grade_type'] = $en ? $en->grade_type : false;
			$this->view_data['eid_hash'] = $enrollment_id;

			$this->load->model('M_student_grades','stud_grades');
			$this->load->model('M_grade_smiley');
			$this->view_data['gd'] = $this->stud_grades->get_grading_system();
			$this->view_data['gs'] = $gs = $this->M_grade_smiley->get_grade_smiley();
			$this->view_data['e_id'] = $enr_id;
			$level_id = ($this->mt->get_section_assigned_grade_level($sec_id));
			if($e)
			{
				//$this->view_data['student_grades'] = $sg = $this->mt->get_student_subjects($sec_id,$enr_id,FALSE); 
				$this->view_data['remarks'] = $this->mt->get_student_remarks_by_enrollment_id($enr_id);
				$this->view_data['attendance'] = $this->mt->get_student_attendance_detailed($enr_id);

				//if(!$sg){
					
					/* GENERATE STUDENT GRADES ON THE FLY */
					$this->stud_grades->generate_grades($enr_id, $level_id);

					$this->view_data['student_grades'] = $this->mt->get_student_subjects($sec_id,$enr_id,FALSE);
				//}
			}

		}else{
			$this->view_data['section_exists'] = FALSE;
			$this->view_data['hash'] = NULL;
			$this->view_data['eid_hash'] = NULL;
			$this->view_data['students'] = ['PLEASE SELECT A SECTION'];
		}

		$this->view_data['sections'] = $this->mt->_get_all_advisory_sections_for_dd();
	}


	public function advisory_progress_report($hash = '',$enrollment_id = '')
	{
		$this->layout_view = 'application_less_sidebar';
		$x = $this->hs->decrypt($hash);
		$e = $this->hs->decrypt($enrollment_id);

		if($x)
		{
			$this->view_data['section_exists'] = TRUE;
			$this->load->model(['M_gperiods','M_sections']);
			$this->view_data['section_desc'] = $this->M_sections->get_all_sections($x[0]);
			$this->view_data['gp'] = $this->M_gperiods->get_gperiods();
			$this->view_data['hash'] = $hash;
			$this->view_data['students'] = $this->mt->get_students($x[0]);
			$this->view_data['eid_hash'] = $enrollment_id;

			if($e)
			{
				$this->view_data['student_grades'] = $this->mt->get_student_subjects($x[0],$e[0],FALSE);
			}

		}else{
			$this->view_data['section_exists'] = FALSE;
			$this->view_data['hash'] = NULL;
			$this->view_data['eid_hash'] = NULL;
			$this->view_data['students'] = ['PLEASE SELECT A SECTION'];
		}

		$this->view_data['sections'] = $this->mt->_get_all_advisory_sections_for_dd();
	}
	
	public function advisory_input_attendance($hash  = '')
	{
		$x = $this->hs->decrypt($hash);
		if($x)
		{
			$this->view_data['section_exists'] = TRUE;
			$this->load->model(['M_gperiods','M_sections']);
			$this->view_data['section_desc'] = $this->M_sections->get_all_sections($x[0]);
			$this->view_data['gp'] = $this->M_gperiods->get_gperiods();
			$this->view_data['hash'] = $hash;

			$this->layout_view = 'application_less_sidebar';
			$this->view_data['attendance'] = $this->mt->get_advisory_students_attendance($x[0]);
		}else{
			$this->view_data['hash'] = NULL;	
			$this->view_data['section_exists'] = NULL;	
		}
		
		$this->view_data['sections'] = $this->mt->_get_all_advisory_sections_for_dd();
	}
	
	public function advisory_input_remarks($hash = '')
	{
		$x = $this->hs->decrypt($hash);
		if($x){
			$this->view_data['section_exists'] = TRUE;
			$this->load->model(['M_gperiods','M_sections']);
			$this->layout_view = 'application_less_sidebar';
			$this->view_data['remarks'] = $this->mt->get_advisory_students_remarks($x[0]);
			$this->view_data['section_desc'] = $this->M_sections->get_all_sections($x[0]);
			$this->view_data['gp'] = $this->M_gperiods->get_gperiods();
			$this->view_data['hash'] = $hash;
		}else{
			$this->view_data['section_exists'] = FALSE;
			$this->view_data['hash'] = NULL;
		}
		
		$this->view_data['sections'] = $this->mt->_get_all_advisory_sections_for_dd();
	}
	
	
	public function set_grades($id)
	{
		$x = $this->hs->decrypt($id);
		
		if($x)
		{
			$section_id = $x[0]; 
			$subject_id = $x[1];
			$this->load->helper('grade');
			$this->load->model('M_gperiods');
			$this->view_data['gp'] = $this->M_gperiods->get_gperiods();
			$this->view_data['student_grades_table'] = $gt = $this->mt->get_student_grade_by_specific_subject_per_section($section_id,$subject_id);
			$this->view_data['student_remarks_table'] = $gt = $this->mt->get_student_grade_by_specific_subject_per_section($section_id,$subject_id);
			$this->view_data['subject_desc'] = $this->mt->get_subject_desc($subject_id);
			$this->view_data['section_desc'] = $sec = $this->mt->get_section_desc($section_id);
			$this->view_data['edit_grade'] = FALSE;
			
			if($this->input->post())
			{
				$post = $this->input->post();
				
				$result = $this->mt->set_student_grades($post);
				
				if($result)
				{
					$this->_msg('s','Updated student grades.','teacher/set_grades/'.$id);
				}else{
					$this->_msg('e','Error. No grades were updated.','teacher/set_grades/'.$id);
				}
			}
		}else{
			$this->_msg('n','System was unable to load grade form table.','teacher/my_schedules');
		}
	}
	
	public function generate_remark($hash = '',$hash2 = '')
	{
		$x = $this->hs->decrypt($hash);
		$y = $this->hs->decrypt($hash2);
		if($x and $y)
		{
			$this->load->model('M_enrollees');
			if($this->M_enrollees->check_enrollment_id(intval($x[0])))
			{
				$this->load->model('M_student_grades');
				if($this->M_student_grades->generate_remarks(intval($x[0])))
				{
					$this->_msg('s','Created Remarks for student.','teacher/advisory_section/'.$hash2.'/'.$hash);
				}else{
					$this->_msg('s','Unable to Create Remarks for student.','teacher/advisory_section'.$hash2.'/'.$hash);
				}
			}else{
				show_404();
			}
		}else{
			show_404();
		}
	}
	
	public function generate_grades($hash = '',$hash2 = '')
	{
		$x = $this->hs->decrypt($hash);
		$y = $this->hs->decrypt($hash2);
		if($x and $y)
		{
			$this->load->model('M_enrollees');
			
			if($this->M_enrollees->check_enrollment_id(intval($x[0])))
			{
				$this->load->model('M_student_subjects');
				if($this->M_student_subjects->generate_student_subjects(intval($x[0]),intval($y[0])))
				{
					$this->load->model('M_student_grades');
					if($this->M_student_grades->generate_student_grades($x[0]))
					{
						$this->_msg('s','Created Remarks for student.','teacher/advisory_section/'.$hash2.'/'.$hash);
					}else{
						$this->_msg('e','Unable to create student grades.','teacher/advisory_section/'.$hash2.'/'.$hash);
					}
				}else{
					$this->_msg('e','Unable to create student grades.','teacher/advisory_section/'.$hash2.'/'.$hash);
				}
			}else{
				
				show_404();
			}
		}else{
			show_404();
		}
	}

	public function generate_attendance($enrollment_id_hash = '',$section_hash = '')
	{
		$x = $this->hs->decrypt($enrollment_id_hash);
		$y = $this->hs->decrypt($section_hash);
		
		if($x AND $y)
		{
			$this->load->model('M_student_attendance');
			if($this->M_student_attendance->generatestudentattendance($x[0]))
			{
				$this->_msg('s','Student attendance successfully created.','teacher/advisory_section/'.$section_hash.'/'.$enrollment_id_hash);
			}else{
				$this->_msg('e','Unable to create attendance table for student.','teacher/advisory_section/'.$section_hash.'/'.$enrollment_id_hash);
			}
		}else{
			show_404();
		}
	}
	
	public function students()
	{
		// pd($this->session);
		// $this->load->model('M_teachers');
		$this->view_data['all_studs'] = $this->mt->get_all_my_students();
	
	}

	public function commendations()
	{	
		$this->_init_pagnate_config();
		$this->load->library('pagination');
		$this->pagnate_config['base_url'] = base_url().'teacher/commendations';
		$this->pagnate_config['total_rows'] = $this->mt->count_sent_commendations();
		$this->pagnate_config['per_page'] = 5;
		$this->pagnate_config["uri_segment"] = 3;
		$this->pagination->initialize($this->pagnate_config);

		$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		$this->view_data['commendations'] = $this->mt->get_all_sent_commendations($this->pagnate_config["per_page"], $page);
		$this->view_data["links"] = $this->pagination->create_links();
	}
}